// my/my.js
import {
  RechargeModel
} from '../../../models/recharge'
import {
  Format
} from '../../../utils/common'
import {
  OrderModel
} from "../../../models/order";
import {
  AccountModel
} from '../../../models/account'
const accountmodel = new AccountModel()
const orderModel = new OrderModel()
const rechargeModel = new RechargeModel()
const app = getApp()
let last
const format = new Format()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    href: '',
    Rechargeactive: '', // 充值送
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(query) {
    // query.amount = '10000'
    console.log('充值结果' + JSON.stringify(query))
    if (query.orderId) {
      this.setData({
        'amount': query.amount,
        'orderId': query.orderId,
        'yhorderID': query.yhorderID,
      })
      console.log(this.data.amount)
      this.loadDetail(query.orderId)
      this.getCouponListByOrderIdOrOpenId(query.yhorderID)
    } else {
      // this.data.amount = '1'
      // this.data.orderId = query.orderId
      query.yhorderID = 'YHCZ100745000'
      this.getCouponListByOrderIdOrOpenId(query.yhorderID)
      // this.loadDetail(query.orderId)
    }
    console.log(query.href)
   if (query.href) {
      this.setData({
        'href': query.href,
      })
   }
    let vm = this
    // rechargeModel.getCRouponListByOrderId(query.orderId).then(res => {
    //   console.log(res)
    //   if (res && res.data && res.data.length > 0) {
    //     vm.setData({
    //       activeName: res.activeName,
    //       couponList: res.data,
    //       resource: res.resource,
    //       equityAmount: res.equityAmount,
    //     });
    //   }
    // })
  },
  loadDetail(tradeNo) {
    orderModel.loadDetail(tradeNo).then(res => {
      console.log('111111' + JSON.stringify(res))
      this.setData({
        detail: res,
      })
    })
  },
  //  获取充值送接口
  getCouponListByOrderIdOrOpenId (orderId) {
    let vm= this
    accountmodel.getCouponListByOrderIdOrOpenId('2',orderId).then(res => {
      console.log(res)
      if (res && res.data && res.data.length > 0) {
        vm.setData({
          Rechargeactive: res,
        })
      }
    })
  },
  toPage(e) {
    const url = e.currentTarget.dataset.url
    if (url == 'oilIndex') {
      // my.alert({
      //   title: '很抱歉',
      //   content: '该功能暂未开通，敬请期待~',
      //   buttonText: '我知道了',
      // });
      if (this.data.href == 'oilConfirm') {
        my.navigateBack({delta: 2})
      } else if (this.data.href == 'oilResult') {
        my.navigateBack({delta: 1})
      } else  {
        my.redirectTo({
          url: '/pages/oil/oilIndex/oilIndex'
        })
      }
    } else if (url == 'backoilConfirm') {
      my.navigateBack({delta: 1})
    } else {
      my.redirectTo({
        url: url + '?activeTab=1'
      })
    }
  },
  toMy(e) {
    const page = e.currentTarget.dataset.url
    my.navigateTo({
      url: page
    })
  },
  onActive() {

  },
  tip(e) {
    const page = e.currentTarget.dataset.url
    my.redirectTo({
      url: page
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // this.loadMyAccount()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})